package com.hdu.controller.utilManagement;

import com.hdu.entity.Music;
import com.hdu.pojo.PageBean;
import com.hdu.pojo.Result;
import com.hdu.service.IMusicService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;


@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "音乐管理接口")
public class MusicController {

    private final IMusicService musicService;

    @GetMapping("/util/music")
    public Result<?> page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "4") Integer limit,
                       String musicName) {
        log.info("分页查询, 参数: {},{},{}", page, limit, musicName);
        //调用service分页查询
        PageBean<Music> pageBean = musicService.page(page, limit, musicName);
        return Result.success(pageBean);
    }


    @GetMapping("/music")
    public Result<?> songList() {
        List<Music> musicList = musicService.list();
        log.info("歌曲查询: {}", musicList);
        return Result.success(musicList);
    }

    @GetMapping("/casual/searchCartoon")
    public Result<?> searchCartoon(String name, String page) throws IOException {
        log.info("动漫列表查询: {},{}", name, page);
        return Result.success(musicService.searchCartoon(name, page));
    }

    @GetMapping("/casual/searchComics")
    public Result<?> searchComics(String name) throws IOException {
        log.info("漫画列表查询: {}", name);
        return Result.success(musicService.searchComics(name));
    }

    @GetMapping("/casual/detailCartoon")
    public Result<?> detailCartoon(String id) throws IOException {
        log.info("动漫详情查询: {}", id);
        return Result.success(musicService.detailCartoon(id));
    }

    @GetMapping("/casual/detailComics")
    public Result<?> detailComics(String id) throws IOException {
        log.info("漫画详情查询: {}", id);
        return Result.success(musicService.detailComics(id));
    }

    @GetMapping("/casual/videoPath")
    public Result<?> videoPath(String id) throws IOException {
        log.info("动漫链接查询: {}", id);
        return Result.success(musicService.videoPath(id));
    }

    @GetMapping("/casual/comicsPath")
    public Result<?> comicsPath(String id, String comicsId) throws IOException {
        log.info("动漫数据查询: {},{}", id, comicsId);
        return Result.success(musicService.comicsPath(id, comicsId));
    }

    @GetMapping("/music/hot")
    public Result<?> hotMusic() {
        List<Music> musicList = musicService.hotMusic();
        return Result.success(musicList);
    }

    @PutMapping("/music/add/{id}")
    public Result<?> update(@PathVariable String id) {
        log.info("更新歌曲，id : {}", id);
        musicService.addCount(id);
        return Result.success();
    }

    @DeleteMapping("/util/music/delete/{ids}")
    public Result<?> delete(@PathVariable List<String> ids) {
        log.info("删除歌曲, idList:{}", ids);
        musicService.removeBatchByIds(ids);
        return Result.success();
    }


    @PostMapping("/util/music/append")
    public Result<?> append(@RequestBody Music music) {
        log.info("新增歌曲, music: {}", music);
        musicService.save(music);
        return Result.success();
    }

    @PutMapping("/util/music/update")
    public Result<?> update(@RequestBody Music music) {
        log.info("更新歌曲, music: {}", music);
        musicService.updateById(music);
        return Result.success();
    }
}
